<template>
  <div class="box-container">
    <div class="box-login">
      <div class="box-img">
        <img src="../assets/jiangnan.png" alt="">
      </div>
      <el-form ref="loginFromRef" :rules="loginFormRules" :model="loginForm" class="login-form" label-width="0px">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" size="mini" @click="login">登录</el-button>
          <el-button type="info" size="mini" @click="resetLoginFrom">重置</el-button>
        </div>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginFrom() {
      this.$refs.loginFromRef.resetFields();
    },
    login() {
      this.$refs.loginFromRef.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post('login', this.loginForm);
        if (res.meta.status !== 200) return this.$message.error('登陆失败');
        this.$message.success('登陆成功');
        // 存储token值到sessionStorage
        // console.log(res);
        window.sessionStorage.setItem("token", res.data.token);
        // 实现页面跳转
        this.$router.push('/home');
      })
    }
  }

}
</script>

<style lang="less" scoped>
.box-container {
  background-image: url(../assets/background.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  height: 100%;

  .box-login {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .box-img {
      width: 130px;
      height: 130px;
      border: 1px solid #ddd;
      border-radius: 50%;
      box-shadow: 0 0 10px #eee;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}

.login-form {
  width: 100%;
  position: absolute;
  bottom: 0;
  padding: 20px;
  box-sizing: border-box;

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>